package com.hluck.goodweatherapp.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hluck.goodweatherapp.data.bean.Province;
import com.hluck.goodweatherapp.databinding.ItemTextRvBinding;
import com.hluck.goodweatherapp.utils.AdministrativeType;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @Author： LJH
 * @Time： 2024/1/4
 * @description：
 */
public class AreaAdapter extends RecyclerView.Adapter<AreaAdapter.ViewHolder> {

    private final List<Province.City.Area> areas;

    private AdministrativeClickCallback administrativeClickCallback;//视图点击


    public AreaAdapter(List<Province.City.Area> areaList) {
        this.areas = areaList;
    }

    public void setAdministrativeClickCallback(AdministrativeClickCallback administrativeClickCallback) {
        this.administrativeClickCallback = administrativeClickCallback;
    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {

        ItemTextRvBinding binding = ItemTextRvBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        ViewHolder viewHolder = new ViewHolder(binding);
        binding.getRoot().setOnClickListener(v -> {
            if (administrativeClickCallback != null){
                administrativeClickCallback.onAdministrativeItemClick(v,viewHolder.getAdapterPosition(), AdministrativeType.AREA);
            }
        });

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull AreaAdapter.ViewHolder holder, int position) {
        holder.binding.tvText.setText(areas.get(position).getAreaName());
    }

    @Override
    public int getItemCount() {
        return areas.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        public ItemTextRvBinding binding;

        public ViewHolder(ItemTextRvBinding itemTextRvBinding){
            super(itemTextRvBinding.getRoot());
            binding = itemTextRvBinding;
        }
    }
}
